package org.erp.admins.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.erp.admins.infrastructure.convert.TenantUserConvert;
import org.erp.admins.domain.entity.TenantUser;
import org.erp.admins.domain.dto.TenantUserDto;
import org.erp.admins.domain.vo.TenantUserVo;
import org.erp.admins.infrastructure.mapper.TenantUserMapper;
import org.erp.admins.service.TenantUserService;
import org.erp.common.page.PageQueryModel;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service("tenantUserService")
public class TenantUserServiceImpl extends ServiceImpl<TenantUserMapper, TenantUser> implements TenantUserService {

    @Resource
    private TenantUserConvert tenantUserConvert;

    @Override
    public PageQueryModel<TenantUserVo> getListByPage(TenantUserDto dto) {
        LambdaQueryWrapper<TenantUser> wrapper = buildListQuery(dto);
        Page<TenantUser> page = this.page(dto.buildPage(), wrapper);
        List<TenantUserVo> voList = page.getRecords().stream()
                .map(tenantUserConvert::entityToVo).collect(Collectors.toList());
        return PageQueryModel.of(page.getPages(), voList);
    }

    @Override
    public boolean updTenantUser(TenantUserDto dto) {
        Assert.notNull(dto.getId(), "ID不能为空");
        Assert.isTrue(StringUtils.hasText(dto.getUserNick()), "会员昵称不能为空");
        TenantUser entity = tenantUserConvert.dtoToEntity(dto);
        return this.updateById(entity);
    }

    @Override
    public boolean addTenantUser(TenantUserDto dto) {
        Assert.isTrue(StringUtils.hasText(dto.getUserNick()), "会员昵称不能为空");
        TenantUser entity = tenantUserConvert.dtoToEntity(dto);
        return this.save(entity);
    }

    @Override
    public TenantUserVo getTenantUserVoById(Long id) {
        TenantUser entity = this.getById(id);
        if (entity != null) {
            return tenantUserConvert.entityToVo(entity);
        }
        return null;
    }

    @Override
    public List<TenantUserVo> getTenantUserVoList(TenantUserDto dto) {
        LambdaQueryWrapper<TenantUser> wrapper = buildListQuery(dto);
        List<TenantUser> list = this.list(wrapper);
        return list.stream().map(tenantUserConvert::entityToVo).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<TenantUser> buildListQuery(TenantUserDto dto) {
        LambdaQueryWrapper<TenantUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(dto.getUserNick()), TenantUser::getUserNick, dto.getUserNick())
                .eq(StringUtils.hasText(dto.getPhone()), TenantUser::getPhone, dto.getPhone())
                .eq(dto.getTenantId() != null, TenantUser::getTenantId, dto.getTenantId())
                .eq(StringUtils.hasText(dto.getStatus()), TenantUser::getStatus, dto.getStatus())
                .eq(TenantUser::getDelFlag, 0)
                .orderByDesc(TenantUser::getCreateTime);
        return wrapper;
    }
}

